package com.community.service;

import com.feng.fengDUtil.BaseService;
import com.community.util.Search;
import com.feng.fengDUtil.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Map;

@Service
@Transactional
public class InfoService extends BaseService{

    public Page query(Page page, Search search) {
        ArrayList params = new ArrayList();
        String sql = "select u.id,time,title,img,second_title from a_news u  where 1=1 ";
        if(StringUtils.isNotEmpty(search.getName())){
            sql+=" and u.title like ? ";
            params.add(processLike(search.getName()));
        }
        sql+=" order by u.sortnum desc,u.time desc";
        findPageList(Map.class,sql,page,params.toArray());
        return page;
    }

    public void setTop(String id) {
        Long maxNum = sql("SELECT ifnull( max( s.sortnum ), 0 ) + 1 FROM a_news s ").one(Long.class);

        sql("update a_news u set u.sortnum = ? where u.id = ?").update(new Object[]{maxNum,id});
    }
}
